package com.ihrm.company.controller;

import com.ihrm.common.controller.BaseController;
import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.company.service.CompanyService;
import com.ihrm.company.service.DepartmentService;
import com.ihrm.domain.company.Company;
import com.ihrm.domain.company.Department;
import com.ihrm.domain.company.response.DeptListResult;
import org.apache.tomcat.util.modeler.BaseModelMBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 13932105637
 * @version l.0
 * @date 2021/7/11 16:12
 * 部门组织管理 Controller层
 * //1.解决跨域
 * CrossOrigin
 * //2.声明restContoller
 * RestController
 * //3.设置父路径
 * RequestMapping(value="/company")   //  company/deparment
 **/
@Controller
@CrossOrigin
@RestController
@RequestMapping(value = "/company")
public class DepartmentController extends BaseController {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private CompanyService companyService;

    /**
     * 保存
     **/
    @RequestMapping(value = "/department", method = RequestMethod.POST)
    public Result save(@RequestBody Department department) {

        //1.设置保存的企业id
        // 企业id：目前使用的固定值
//        String companId = "1";
        department.setCompanyId(this.companyId);

        //2.调用service完成作业
        departmentService.save(department);

        //3.构造返回结果
        return new Result(ResultCode.SUCCESS);

    }

    /**
     * 根据ID查询department
     */
    @RequestMapping(value = "/department", method = RequestMethod.GET)
    public Result findAll() {
        //1.指定企业id
//        String companId = "1";
        Company company = companyService.findById(this.companyId);
        //2.完成查询
        List department = departmentService.findAll(this.companyId);
        //3.构造返回参数
        return new Result(ResultCode.SUCCESS, new DeptListResult(company, department));
    }

    /**
     * 修改Department
     */
    @RequestMapping(value="/department/{id}",method = RequestMethod.PUT)
    public Result update(@PathVariable(value="id") String id,@RequestBody Department department) {
        //1.设置修改的部门id
        department.setId(id);
        //2.调用service更新
        departmentService.update(department);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据id删除
     */
    @RequestMapping(value="/department/{id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable(value="id") String id) {
        departmentService.deleteById(id);
        return new Result(ResultCode.SUCCESS);
    }
















}
